<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>气象信息</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }
        
        .weather {
            position: relative;
            width: 600px;
            height: 220px;
            margin: 0 auto;
            background: linear-gradient(#1C2A47, #6B5F6A);
            text-align: center;
            color: #fff;
            font-size: 18px;
            font-weight: 700;
            border-radius: 20px;
        }
        
        .left {
            width: 120px;
            height: 50%;
            float: left;
            margin-top: 10px;
        }
        
        .right {
            width: 130px;
            height: 50%;
            float: right;
            margin-top: 10px;
        }
        
        img {
            width: 40px;
            height: 40px;
            background-color: #fff;
            border-radius: 6px;
        }
        
        .time {
            box-sizing: border-box;
            position: absolute;
            bottom: 0;
            width: 100%;
            height: 135px;
            padding-top: 20px;
        }
        
        .top_time,
        .top_temp,
        .weather_info,
        .min_temp,
        .max_temp {
            font-weight: 300;
            font-family: 'Consolas';
            font-size: 14px;
        }
        
        .top_weather_info {
            margin: 3px 0 3px 0;
        }
        /*  header --------------*/
        
        header {
            position: fixed;
            top: 0;
            right: 5px;
            background-color: #fff;
            font-family: 'Consolas';
            border-radius: 10px;
            margin-right: 10px;
        }
        
        header div {
            display: inline-block;
            vertical-align: middle;
        }
        
        .location_city {
            font-weight: 700;
        }
        
        #location {
            width: 30px;
            height: 30px;
            margin-right: -8px;
        }
        
        #location img {
            width: 100%;
            height: 100%;
        }
        
        #location:hover {
            cursor: pointer;
        }
        /*  --------------header */
        
        .verification_code {
            border-radius: 10px;
            width: 300px;
            margin: 0 auto;
            padding: 5px;
            background-color: #fff;
        }
        
        .title {
            text-align: center;
            font-family: 'Consolas';
            font-weight: 700;
            margin-bottom: 10px;
        }
        
        #canvas {
            width: 150px;
            vertical-align: bottom;
        }
        
        .code input[type="text"] {
            padding-left: 10px;
            width: 40%;
            height: 40px;
            border: 1px solid #ccc;
            outline: none;
        }
        
        .code input[type="button"] {
            margin-top: 10px;
            display: block;
            width: 85%;
            height: 40px;
            background-color: #FE963C;
            border: 0;
            color: #fff;
            font-weight: 700;
        }
        
        .code input[type="button"]:hover {
            cursor: pointer;
            opacity: 0.8;
        }
    </style>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="./js/coco-message.js"></script>
    <script>
        //展示信息
        function example(n) {
            let div = document.createElement("div");
            switch (n) {
                case 0:
                    cocoMessage.info(1000, "请输入验证码！", function() {

                    });
                    break;

                case 1:
                    div.innerText = "验证码校验成功！";
                    cocoMessage.success(div);
                    break;

                case 2:
                    cocoMessage.warning("每秒并发请求200次！,请求上限20w次！", 0);
                    break;

                case 3:
                    cocoMessage.error("验证码错误！请重新输入！", 1000);
                    break;

                case 4:
                    var closeMsg = cocoMessage.loading(true);
                    setTimeout(function() {
                        closeMsg();
                    }, 1000);
                    break;

                case 5:
                    cocoMessage.destroyAll();
                    break;

                default:
                    break;
            }
        }
        // 获取定位信息查询天气信息
        function getIpAddressWeather() {
            let key = "c9d5eeb541fdef2dc667c7e7132f715d";
            //定位
            $.ajax({
                type: 'get',
                dataType: 'json',
                url: 'https://restapi.amap.com/v3/ip?key=' + key,
                success: function(ipdata) {
                    // 返回城市编码
                    let citycode = ipdata.adcode;
                    console.log(citycode);
                    // 请求天气
                    $.ajax({
                        type: "GET",
                        dataType: "json",
                        url: "https://restapi.amap.com/v3/weather/weatherInfo?key=" + key + "&city=" + citycode + "&extensions=base&output=json",
                        success: function(data) {
                            if (data.infocode == 10000) {
                                let live = data.lives[0];
                                $('.city').text(live.city);
                                $('.temp').text(live.temperature + '°');


                                $('.centerimg img').attr('src', './img/' + live.weather + '.png');
                                console.log("天气：" + live.weather);
                                $('.top_weather_info img').attr('src', './img/' + live.weather + '.png');
                                $('.weather_info').text(live.weather);
                                $('.max_temp').text('最低 - °');
                                $('.min_temp').text('最高 - °');

                                let startdate = live.reporttime.split(' ')[1];
                                let date = startdate.split(':')[0];
                                let show_time = '';
                                if (date > 0 && date <= 12) {
                                    show_time = '上午';
                                } else {
                                    show_time = '下午'
                                }
                                show_time += date - 12;

                                $('.top_time').text(show_time + '时');

                                $('.top_temp').text(live.temperature + '°');

                                // -------------------------------

                                $('.location_city').text(live.city);
                                $('.position_img img').attr('src', './img/' + live.weather + '.png');
                                $('#location_temp').text(live.temperature + '°');
                                $('#location_weather').text(live.weather);
                            } else {
                                $('header').html('<h2>遇到错误!</h2>');
                                console.log('错误码：' + data.status);
                            }
                        },
                        error: function(res) {
                            $('body').html(res.responseText);
                        }
                    });
                },
                error: function(res) {
                    $('body').html(res.responseText);
                }
            });
        }

        function draw(show_num) { //显示区域内容信息
            var canvas_width = $('#canvas').width();
            var canvas_height = $('#canvas').height();
            var canvas = document.getElementById("canvas"); //获取到canvas的对象，演员
            var context = canvas.getContext("2d"); //获取到canvas画图的环境，演员表演的舞台
            canvas.width = canvas_width;
            canvas.height = canvas_height;
            var sCode =
                "a,b,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,W,X,Y,Z,1,2,3,4,5,6,7,8,9,0";
            var aCode = sCode.split(",");
            var aLength = aCode.length; //获取到数组的长度

            for (var i = 0; i <= 3; i++) {
                var j = Math.floor(Math.random() * aLength); //获取到随机的索引值
                var deg = Math.random() * 30 * Math.PI / 180; //产生0~30之间的随机弧度
                var txt = aCode[j]; //得到随机的一个内容
                show_num[i] = txt.toLowerCase();
                var x = 10 + i * 20; //文字在canvas上的x坐标
                var y = 20 + Math.random() * 8; //文字在canvas上的y坐标
                context.font = "bold 23px 微软雅黑";

                context.translate(x, y);
                context.rotate(deg);

                context.fillStyle = randomColor();
                context.fillText(txt, 0, 0);

                context.rotate(-deg);
                context.translate(-x, -y);
            }
            for (var i = 0; i <= 5; i++) { //验证码上显示线条
                context.strokeStyle = randomColor();
                context.beginPath();
                context.moveTo(Math.random() * canvas_width, Math.random() * canvas_height);
                context.lineTo(Math.random() * canvas_width, Math.random() * canvas_height);
                context.stroke();
            }
            for (var i = 0; i <= 30; i++) { //验证码上显示小点
                context.strokeStyle = randomColor();
                context.beginPath();
                var x = Math.random() * canvas_width;
                var y = Math.random() * canvas_height;
                context.moveTo(x, y);
                context.lineTo(x + 1, y + 1);
                context.stroke();
            }
        }

        function randomColor() { //得到随机的颜色值
            var r = Math.floor(Math.random() * 256);
            var g = Math.floor(Math.random() * 256);
            var b = Math.floor(Math.random() * 256);
            return "rgb(" + r + "," + g + "," + b + ")";
        }


        $(function() {
            //验证码校验
            function verifyCode() {
                var val = $("#inputCode").val().toLowerCase();
                var num = show_num.join("");
                if (val == '') {
                    example(0);
                } else if (val == num) {
                    example(1);
                    getIpAddressWeather();
                    $("#inputCode").val('');
                    draw(show_num);
                } else {
                    example(3);
                    $("#inputCode").val('');
                    draw(show_num);
                }
            }
            // 页面第一次加载自动获取一次天气信息
            getIpAddressWeather();
            example(2);

            var show_num = [];
            draw(show_num);

            $("#canvas").on('click', function() {
                draw(show_num);
            });
            //点击定位图标
            $("#location").on('click', function() {
                verifyCode();
            });
            //单击事件的时候校验输入的验证码是否正确
            $("#confirmBtn").on('click', function() {
                verifyCode();
            });
        })
    </script>
</head>

<body>
    <div class="weather">
        <div class="left">
            <div class="city"></div>
            <div class="temp"></div>
        </div>
        <div class="right">
            <div class="centerimg">
                <img src="" alt="">
            </div>
            <div class="weather_info"></div>
            <span class="min_temp">最低°</span>
            <span class="max_temp">最高°</span>
        </div>
        <div class="time">
            <div class="info">
                <div class="top_time">下午1时</div>
                <div class="top_weather_info">
                    <img src="" alt="">
                </div>
                <div class="top_temp">14°</div>
            </div>
        </div>
    </div>
    <div class="verification_code">
        <div class="title">请输入图形验证码</div>
        <div class="code">
            <input type="text" placeholder="请输入" id="inputCode">
            <canvas id="canvas" width="150px" height="43px"></canvas>
            <input type="button" name="" id="confirmBtn" value="确定">
        </div>
    </div>

    <header>
        <div id="location">
            <img src="./img/location.png" alt="">
        </div>
        <div id="city" class="location_city"></div>
        <div class="position_img">
            <img src="" alt="">
        </div>
        <div id="location_temp"></div>
        <div id="location_weather"></div>
    </header>
</body>

</html>